package pack1015;

import java.util.Scanner;

/*
ATM自动存取款机
1. 登陆
2. 存款，必须是100元面值
3. 取款，必须是100元面值，并且单次取款不能高于5000，并且不能大于余额
4. 转账功能，输入对方卡号和转账金额实现转账，不能大于余额，不能高于5000
5. 查询余额功能
6. 修改密码功能
7. 退出系统功能
本系统要求友好，大方，美观，逻辑流畅
 */
// 这个类是一个机器：登陆、存款、取款、转账、查询、修改密码和退出
public class ATM {
    // 这是什么？
    Card[] cards;
    Scanner scan;
    // 密码输入错误次数
    int errorCount;
    // 当前登陆的用户信息
    Card loginCard;

    // 构造器
    public ATM() {
        // 初始化属性
        cards = new Card[10];
        int no = 1001;
        int password = 123456;
        for (int i = 0; i < cards.length; i++) {
            int money = (int) (Math.random() * 9990 + 10);
            Card card = new Card(no, password, money);
            no++;
            cards[i] = card;

        }
        this.scan = new Scanner(System.in);
    }

    // 登陆操作：连续三次登陆失败，吞卡
    public void login() {
        System.out.print("请插入卡：");
        int cardNo = scan.nextInt();
        System.out.print("请输入密码：");
        int password = scan.nextInt();
        // 和数组中的每一个卡进行比较
        boolean flag = false; // 失败
        for (int i = 0; i < cards.length; i++) {
            // 得到每一个卡片
            Card c = cards[i];
            if (cardNo == c.card && password == c.password) {
                flag = true;
                this.loginCard = c;
                break;
            }
        }
        if (flag) {
            // 登陆成功
            System.out.println("恭喜你，登陆成功！");
            welcome();
        } else {
            System.out.println("你的密码错误，请查证后输入");
            errorCount++;
            if (errorCount < 3) {
                login();
            } else {
                System.out.println("密码连续错误三次，吞卡！");
                return;
            }
        }
    }

    // 主页面
    public void welcome() {
        System.out.println("************************************************************");
        System.out.println("*****************欢迎访问中国XX银行ATM系统********************");
        System.out.println("************************************************************");
        System.out.println("1.查询");
        System.out.println("2.存款");
        System.out.println("3.转账");
        System.out.println("4.取款");
        System.out.println("5.改密");
        System.out.println("6.退出");
        System.out.print("请选择你的操作：");
        String type = scan.next();
        switch (type) {
            case "查询", "1" -> find();
            case "存款", "2" -> deposit();
            case "转账", "3" -> transfer();
            case "改密", "5" -> changePass();
            case "取款", "4" -> withdrawMoney();
            case "退出", "6" -> exit();
            default -> {
                System.out.println("输入错误");
                welcome();
            }
        }
    }

    public void find() {
        System.out.println("你" + loginCard.card + "的余额是：" + loginCard.money);
        System.out.println("开始返回主界面");
        welcome();
    }

    public void deposit() {
        System.out.print("请输入存款金额：");
        int money = scan.nextInt();
        if (money % 100 != 0) {
            System.out.println("本ATM机只支持100元面值钞票！");
            deposit();
            return;
        }
        loginCard.money += money;
        System.out.println("存款成功！");
        welcome();
    }

    public void transfer() {
        System.out.print("请输入对方卡号：");
        int cardNo = scan.nextInt();
        System.out.print("请输入转账金额：");
        int money = scan.nextInt();

        if (money > 5000) {
            System.out.println("单次转账金额不能超过5000元");
            transfer();
            return;
        }
        if (money > loginCard.money) {
            System.out.println("余额不足");
            transfer();
            return;
        }
        for (int i = 0; i < cards.length; i++) {
            if (cardNo == cards[i].card) {
                cards[i].money += money;
                break;
            }
        }
        this.loginCard.money -= money;
        System.out.println("转账成功");
        welcome();
    }

    public void withdrawMoney() {
        System.out.print("请输入取款金额：");
        int money = scan.nextInt();
        if (money % 100 != 0) {
            System.out.println("本ATM机只支持100元面值钞票！");
            withdrawMoney();
            return;
        }
        if (money > 5000) {
            System.out.println("单次取款金额不能超过5000元");
            withdrawMoney();
            return;
        }
        if (money > loginCard.money) {
            System.out.println("余额不足");
            withdrawMoney();
            return;
        }
        loginCard.money -= money;
        System.out.println("取款成功");
        welcome();
    }

    public void exit() {
        System.out.println("欢迎下次光临，再见");
    }

    public void changePass() {
        System.out.print("请输入原始密码：");
        int oldPassword = scan.nextInt();
        System.out.print("请输入新密码：");
        int newPassword1 = scan.nextInt();
        System.out.print("请再次输入新密码：");
        int newPassword2 = scan.nextInt();
        if (oldPassword != loginCard.password) {
            System.out.println("原始密码输入错误，请查证后输入");
            welcome();
            return;
        }
        if (newPassword1 != newPassword2) {
            System.out.println("两次密码输入不一致，请查证后输入");
            welcome();
            return;
        }
        this.loginCard.password = newPassword1;
        System.out.println("修改密码成功");
        login();
    }
}